/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.platform.dto.EquiomentAlarmLogDTO;
import org.springblade.modules.platform.entity.EquiomentAlarmLog;
import org.springblade.modules.platform.mapper.EquiomentAlarmLogMapper;
import org.springblade.modules.platform.service.IEquiomentAlarmLogService;
import org.springblade.modules.platform.vo.EquiomentAlarmLogListVO;
import org.springblade.modules.platform.vo.EquiomentAlarmLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-11-25
 */
@Service
public class EquiomentAlarmLogServiceImpl extends ServiceImpl<EquiomentAlarmLogMapper, EquiomentAlarmLog> implements IEquiomentAlarmLogService {

	@Override
	public IPage<EquiomentAlarmLogVO> selectEquiomentAlarmLogPage(IPage<EquiomentAlarmLogVO> page, EquiomentAlarmLogVO equiomentAlarmLog) {
		return page.setRecords(baseMapper.selectEquiomentAlarmLogPage(page, equiomentAlarmLog));
	}

	@Override
	public EquiomentAlarmLogListVO selectPage1(EquiomentAlarmLogDTO equiomentAlarmLogDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		EquiomentAlarmLogListVO equiomentAlarmLogListVO = new EquiomentAlarmLogListVO();
		QueryWrapper<EquiomentAlarmLog> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(EquiomentAlarmLog::getIsDeleted,0)
			.eq(Func.isNotEmpty(user),EquiomentAlarmLog::getTenantId,user.getTenantId());
		if(!StringUtils.isEmpty(equiomentAlarmLogDTO.getAlarmId())){
			queryWrapper.lambda().eq(EquiomentAlarmLog::getAlarmId,equiomentAlarmLogDTO.getAlarmId());
		}
		if(!StringUtils.isEmpty(equiomentAlarmLogDTO.getProductId())){
			queryWrapper.lambda().eq(EquiomentAlarmLog::getProductId,equiomentAlarmLogDTO.getProductId());
		}
		if(!StringUtils.isEmpty(equiomentAlarmLogDTO.getEquipmentId())){
			queryWrapper.lambda().eq(EquiomentAlarmLog::getEquipmentId,equiomentAlarmLogDTO.getEquipmentId());
		}
		if(equiomentAlarmLogDTO.getIsProductAlarm() != null){
			queryWrapper.lambda().eq(EquiomentAlarmLog::getIsProductAlarm,equiomentAlarmLogDTO.getIsProductAlarm());
		}
		if(equiomentAlarmLogDTO.getStartDate()!=null && equiomentAlarmLogDTO.getEndDate() != null ){
			queryWrapper.lambda().between(EquiomentAlarmLog::getAlarmTime,equiomentAlarmLogDTO.getStartDate(),equiomentAlarmLogDTO.getEndDate());
		}
		queryWrapper.lambda().orderByDesc(EquiomentAlarmLog::getAlarmTime);
		IPage<EquiomentAlarmLog> equiomentAlarmLogIPage = baseMapper.selectPage(Condition.getPage(query), queryWrapper);
		equiomentAlarmLogListVO.setTotal(equiomentAlarmLogIPage.getTotal());
		if(!CollectionUtils.isEmpty(equiomentAlarmLogIPage.getRecords())){
			List<EquiomentAlarmLogVO> collect = equiomentAlarmLogIPage.getRecords().stream().map(x -> {

				EquiomentAlarmLogVO equiomentAlarmLogVO = new EquiomentAlarmLogVO();
				BeanUtils.copyProperties(x, equiomentAlarmLogVO);
				return equiomentAlarmLogVO;
			}).collect(Collectors.toList());
			equiomentAlarmLogListVO.setLogVOS(collect);
		}
		return equiomentAlarmLogListVO;
	}

}
